package com.tudouji.project.farmer.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.common.utils.sql.SqlUtil;
import com.tudouji.framework.web.page.PageDomain;
import com.tudouji.framework.web.page.TableSupport;
import com.tudouji.project.permission.domain.Salespermission;
import com.tudouji.project.permission.service.ISalespermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.farmer.mapper.FarmerorderMapper;
import com.tudouji.project.farmer.domain.Farmerorder;
import com.tudouji.project.farmer.service.IFarmerorderService;

/**
 * 种植订单Service业务层处理
 * 
 * @author tudouji
 * @date 2021-04-25
 */
@Service
public class FarmerorderServiceImpl implements IFarmerorderService 
{
    @Autowired
    private FarmerorderMapper farmerorderMapper;
    @Autowired
    private ISalespermissionService permissionService;

    /**
     * 查询种植订单
     * 
     * @param guid 种植订单ID
     * @return 种植订单
     */
    @Override
    public Farmerorder selectFarmerorderById(String guid)
    {
        return farmerorderMapper.selectFarmerorderById(guid);
    }

    /**
     * 查询种植订单列表
     * 
     * @param farmerorder 种植订单
     * @return 种植订单
     */
    @Override
    public List<Farmerorder> selectList(Farmerorder farmerorder, String tuserId)
    {
            return farmerorderMapper.selectFarmerorderList(farmerorder);
    }
    /**
     * 查询种植订单列表
     *
     * @param farmerorder 种植订单
     * @return 种植订单
     */
    @Override
    public List<Farmerorder> selectFarmerorderList(Farmerorder farmerorder, String tuserId) throws Exception
    {
        // 查询用户是否有全局权限
        List<Salespermission> permissions = permissionService.getPermByScAndFunc(tuserId, "24", "4");
        int flag = 0;
        if (permissions.size() < 1) {// 没有全局权限
            // 渠道
            List<Salespermission> departperms = permissionService.getPermByScAndFunc(tuserId, "24", "2");
            if (departperms.size() > 0) {
                flag = 1;
                String[] departids = new String[departperms.size()];
                for (int i = 0; i < departperms.size(); i++) {
                    departids[i] = departperms.get(i).getDepartid();
                }
                farmerorder.setDepartids(departids);
                flag = 1;
            }
        } else {
            flag = 1;
        }
        if (flag == 1) {
            startPage();
            return farmerorderMapper.selectFarmerorderList(farmerorder);
        }
        return new ArrayList<Farmerorder>();

    }

    /**
     * 查询种植订单列表
     *
     * @param farmerorder 种植订单
     * @return 种植订单
     */
    @Override
    public List<Farmerorder> export(Farmerorder farmerorder, String tuserId) throws Exception
    {
        // 查询用户是否有全局权限
        List<Salespermission> permissions = permissionService.getPermByScAndFunc(tuserId, "24", "4");
        int flag = 0;
        if (permissions.size() < 1) {// 没有全局权限
            // 渠道
            List<Salespermission> departperms = permissionService.getPermByScAndFunc(tuserId, "24", "2");
            if (departperms.size() > 0) {
                flag = 1;
                String[] departids = new String[departperms.size()];
                for (int i = 0; i < departperms.size(); i++) {
                    departids[i] = departperms.get(i).getDepartid();
                }
                farmerorder.setDepartids(departids);
                flag = 1;
            }
        } else {
            flag = 1;
        }
        if (flag == 1) {
            return farmerorderMapper.selectFarmerorderList(farmerorder);
        }
        return new ArrayList<Farmerorder>();

    }
    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }
    /**
     * 新增种植订单
     * 
     * @param farmerorder 种植订单
     * @return 结果
     */
    @Override
    public int insertFarmerorder(Farmerorder farmerorder)
    {
        return farmerorderMapper.insertFarmerorder(farmerorder);
    }

    /**
     * 修改种植订单
     * 
     * @param farmerorder 种植订单
     * @return 结果
     */
    @Override
    public int updateFarmerorder(Farmerorder farmerorder)
    {
        return farmerorderMapper.updateFarmerorder(farmerorder);
    }

    /**
     * 批量删除种植订单
     * 
     * @param guids 需要删除的种植订单ID
     * @return 结果
     */
    @Override
    public int deleteFarmerorderByIds(String[] guids)
    {
        return farmerorderMapper.deleteFarmerorderByIds(guids);
    }

    /**
     * 删除种植订单信息
     * 
     * @param guid 种植订单ID
     * @return 结果
     */
    @Override
    public int deleteFarmerorderById(String guid)
    {
        return farmerorderMapper.deleteFarmerorderById(guid);
    }
}
